<template>
  <div>
    <el-form :model="forms" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="资源名称" prop="name">
        <el-input v-model="forms.name" placeholder="请输入资源名称"></el-input>
      </el-form-item>

      <el-form-item label="前端url" prop="frontUrl">
        <el-input
          v-model="forms.frontUrl"
          placeholder="请输入前端url"
        ></el-input>
      </el-form-item>

      <!-- prop代表验证 -->
      <el-form-item label="后端url" prop="backUrl">
        <el-input
          v-model="forms.backUrl"
          placeholder="请输入后端url"
        ></el-input>
      </el-form-item>

      <el-form-item label="资源类型">
        <el-radio-group v-model="forms.type">
          <el-radio :label="1">目录</el-radio>
          <el-radio :label="0">按钮</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button size="small" type="primary" @click="save" plain
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import resourceApi from "@/api/user/resource";
import { ElMessage } from "element-plus";
export default {
  name: "ResourceEdit",
  props: {
    show: {
      type: Boolean,
    },
    editId: {
      type: Number,
    },
    parentId: {
      type: Number,
    },
  },
  created() {
    //根据有没有id来判断是修改还是添加
    if (this.editId) {
      /* this.myGet(this.url.getone, { id: this.editId }, (response) => {
        for (let key in this.forms) {
          this.forms[key] = response.obj[key];
        }
        this.forms.id = this.editId;
      }); */
    } else {
      //如果是添加
      this.forms.parentId = this.parentId;
    }
  },
  data() {
    //自定义验证规则
    const checkData = async (rule, value, callback) => {
      // 注意axios是异步操作
      const result = await this.sync_request(this.url.check, "get", {
        value: value,
        id: this.editId,
      });
      if (result.data.obj) {
        callback();
      } else {
        callback(new Error("存在重复的数据"));
      }
    };

    return {
      // 逻辑路径统一封装
      url: {
        check: "/ums-resource/check",
        edit: "/ums-resource/edit",
        getone: "/ums-resource/getone",
      },
      forms: {
        id: "",
        name: "",
        frontUrl: "",
        backUrl: "",
        parentId: null,
        type: 1, //默认1，代表目录
      },
      rules: {
        name: [
          { required: true, message: "请输入资源名", trigger: "blur" },
          { min: 1, max: 20, message: "长度在1-20之间", trigger: "change" },
          { validator: checkData, trigger: "blur" },
        ],
        frontUrl: [
          { min: 0, max: 255, message: "长度在1-255之间", trigger: "change" },
        ],
        backUrl: [
          { min: 0, max: 255, message: "长度在1-255之间", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    save() {
      //提交表单前需要对表单再次进行验证
      //获取表单对象
      //axios只有异步没有同步

      this.$refs["ruleForm"].validate((flag) => {
        //如果通过验证，则进行表单数据提交
        resourceApi.add(this.forms).then((res) => {
          if (res.data.code === 200) {
            ElMessage.success("添加成功");
            this.$emit("getTable");
            this.$emit("update:show", false);
          }
        });
      });
    },
  },
};
</script>

<style></style>
